package com.kang.controller;

import com.kang.entity.Essay;
import com.kang.service.BookMarkService;
import com.kang.util.WebResponse;
import com.kang.vo.ParamInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "收藏接口")
@RequestMapping("/user/bookMark")
@Slf4j
public class BookMarkController {

    @Autowired
    private BookMarkService bookMarkService;

    @ApiOperation("收藏")
    @PostMapping("insertBookMark")
    public WebResponse insertBookMark(HttpServletRequest request,@RequestBody Essay essay) {
        try {
            return WebResponse.resSuccess("收藏成功",bookMarkService.insertBookMark(request,essay.getId()));
        } catch (Exception ex) {
            log.error("收藏失败",ex);
            return WebResponse.resFail("收藏失败",null);
        }
    }

    @ApiOperation("取消收藏")
    @PostMapping("updateBookMark")
    public WebResponse updateBookMark(HttpServletRequest request,@RequestBody Essay essay) {
        try {
            return WebResponse.resSuccess("取消收藏成功",bookMarkService.updateBookMark(request,essay.getId()));
        } catch (Exception ex) {
            log.error("取消收藏失败",ex);
            return WebResponse.resFail("取消收藏失败",null);
        }
    }


    @ApiOperation("查看收藏列表")
    @GetMapping("getBookMarkList")
    public WebResponse getBookMarkList(ParamInfo paramInfo) {
        try {
            return WebResponse.resSuccess("查询文章列表成功",bookMarkService.getBookMarkList(paramInfo));
        } catch (Exception ex) {
            log.error("查看收藏列表失败",ex);
            return WebResponse.resFail("查看收藏列表失败",null);
        }
    }

}
